//
//  TPHomeCell.m
//  Yimei
//
//  Created by xiaowu xiaowu on 2017/10/22.
//  Copyright © 2017年 xiaowu xiaowu. All rights reserved.
//

#import "TPHomeCell.h"
#import "TPHomeModel.h"
@interface TPHomeCell()
@property (nonatomic,strong)UIView *tagViews;
@property (nonatomic,strong)UIView *bottomView;
@end
@implementation TPHomeCell

-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    if(self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]){
        [self setup];
    }
    return self;
}
-(void)setup{
    
    self.backgroundColor = [UIColor whiteColor];
    
    UIView *bView = [[UIView alloc]init];
    bView.backgroundColor = TP_COLOR_245;
    [self.contentView addSubview:bView];
    [bView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.left.right.mas_equalTo(0.0);
        make.height.mas_equalTo(7.0);
        
    }];
    
    _bottomView = [[UIView alloc]init];
    [self.contentView addSubview:_bottomView];
    [_bottomView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.mas_equalTo(bView.mas_top);
        make.height.mas_equalTo(38.0);
        make.left.mas_equalTo(10.0);
        make.right.mas_equalTo(-10.0);
    }];
    [_bottomView tp_addLineTopWithColor:TP_COLOR_240];
    
    self.image_view.contentMode = UIViewContentModeScaleAspectFill;
    [self.image_view mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.mas_equalTo(_bottomView.mas_top).offset(-5.0);
        make.width.height.mas_equalTo(60.0);
        make.left.mas_equalTo(10.0);
        make.top.mas_equalTo(7.0);
    }];
    self.title_label.numberOfLines = 2;
    [self.title_label mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.image_view.mas_right).offset(10.0);
        make.top.mas_equalTo(self.image_view.mas_top);
        make.right.mas_equalTo(-10.0);
    }];
    
    _tagViews = [[UIView alloc]init];
    [self.contentView addSubview:_tagViews];
    [_tagViews mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.title_label.mas_left);
        make.right.mas_equalTo(self.title_label.mas_right);
        make.bottom.mas_equalTo(self.image_view.mas_bottom);
        make.height.mas_equalTo(30.0);
    }];
    
    UIButton *testButton;
    for(NSInteger i = 0; i<3;i++){
        UIButton *button = [UIButton buttonWithFrame:CGRectZero title:@"" font:fontSystemOfSize(12) color:[UIColor whiteColor]];
        button.hidden = YES;
        button.userInteractionEnabled = NO;
        [button setBackgroundImage:[ImageNamed(@"Home_cell_tag_bg") resizableImageWithCapInsets:UIEdgeInsetsMake(0, 20, 0, 0)] forState:UIControlStateNormal];
        [_tagViews addSubview:button];
        button.tag = i+1000;
        [button mas_makeConstraints:^(MASConstraintMaker *make) {
            if(!testButton)
            make.left.mas_equalTo(self.title_label.mas_left);
            else
                make.left.mas_equalTo(testButton.mas_right).offset(20.0);
            make.bottom.mas_equalTo(0.0);
            make.height.mas_equalTo(15);
        }];
        testButton = button;
    }
    
    
    UILabel *testLabel;
    for(NSInteger i = 0; i<3;i++){
        UILabel *label = [UILabel initWithFrame:CGRectZero font:fontSystemOfSize(13) color:TPBlack_999999];
        label.userInteractionEnabled = NO;
        label.textAlignment = NSTextAlignmentCenter;
        [_bottomView addSubview:label];
        label.tag = i+2000;
        [label mas_makeConstraints:^(MASConstraintMaker *make) {
            if(!testLabel)
                make.left.mas_equalTo(self.image_view.mas_left);
            else
              make.left.mas_equalTo(testLabel.mas_right);
            
            make.bottom.mas_equalTo(0.0);
            make.top.mas_equalTo(0.0);
            make.width.mas_equalTo(_bottomView.mas_width).multipliedBy(0.333);
        }];
        testLabel = label;
    }
    
    
    
}
-(void)setModel:(TPHomeModel *)model{
    
    _model = model;
    
//    if(model == nil){
//        model = [TPHomeModel new];
//        model = model.homes[self.indexPath.row%model.homes.count];
//    }
    
    [self.image_view ym_setImageWithURL:model.goods_image placeholderImage:defaultLogo];
    self.title_label.text = model.goods_name;
    
    for(NSInteger i =0;i<3;i++){
        UIButton *button = [self.tagViews viewWithTag:i+1000];
        button.hidden = YES;
    }
    for(NSInteger i =0;i<model.effects.count;i++){
        NSDictionary *dict =model.effects[i];
        UIButton *button = [self.tagViews viewWithTag:i+1000];
        [button setTitle:xw_getNULLString(dict[@"l_name"]) forState:0];
        button.hidden = NO;
    }
    
    for(NSInteger i =0;i<3;i++){
        UILabel *label = [self.bottomView viewWithTag:i+2000];
        switch (i) {
            case 0:
                label.text = [NSString stringWithFormat:@"参考价:￥%@",model.goods_price];
                break;
            case 1:{
                
                label.hidden = self.is_collection;
                label.text = [NSString stringWithFormat:@"疗程价:￥%@",model.course_price];
                
            }
                break;
            case 2:
                label.text = [NSString stringWithFormat:@"预约量:%ld",model.appoint_number];
                break;
                
            default:
                break;
        }
        
    }
}

@end
